package com.fadadianqi.mall.web.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.mall.exception.MemberException;
import com.fadadianqi.mall.model.Member;
import com.fadadianqi.mall.service.MemberService;
import com.yeqiangwei.commons.ip.IPUtils;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.FormUtils;
import com.yeqiangwei.commons.util.ParamHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@Controller("memberRegController")
public class RegController {

    private static final Log log = LogFactory.getLog(RegController.class);

    @RequestMapping("/member/reg")
    public String showForm(HttpServletRequest request, ModelMap modelMap) {

        return "/member/reg";
    }

    @RequestMapping("/member/reg/ajax/validate")
    public ResponseEntity<String> ajaxValidate(HttpServletRequest request, ModelMap modelMap) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        Boolean isSucceed = Boolean.TRUE;
        String errCode = null;
        String n = ParamHelper.getString(request, "n", null);
        String val = ParamHelper.getString(request, "v", null);
        Member member = null;
        if ("email".equals(n)) {
            member = memberService.getByEmail(val);
            if (null != member) {
                errCode = "exception.member.reg.duplicate.email";
            }
        } else if ("name".equals(n)) {
            member = memberService.getByName(val);
            if (null != member) {
                errCode = "exception.member.reg.duplicate.name";
            }
        } else if ("mobile".equals(n)) {
            member = memberService.getByMobile(val);
            if (null != member) {
                errCode = "exception.member.reg.duplicate.mobile";
            }
        }
        String errMsg = null;
        if (null != errCode) {
            isSucceed = Boolean.FALSE;
            errMsg = ms.getMessage(errCode, null, null, Locale.getDefault());
        }
        AjaxResult rs = new AjaxResult(null, isSucceed, errMsg);
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    @RequestMapping("/member/reg/ajax/create")
    public ResponseEntity<String> ajaxCreate(HttpServletRequest request, ModelMap modelMap) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        //String json = ParamUtils.getString(request, "json", null);
        /*if (log.isDebugEnabled()) {
			log.debug("json: " + json);
		}
		Object JSON = JSONProvider.toBean(json, MemberForm.class);
		MemberForm form = (MemberForm) JSON;*/
        MemberForm form = new MemberForm();
        FormUtils.formBuilder(request, form);
        Member member = new Member();
        BeanUtils.copyProperties(member, form);
        member.setIpAddr(IPUtils.getIpAddr(request));
        member.setIpCode(IPUtils.enIpAddr(member.getIpAddr()));
        Boolean isSucceed = Boolean.TRUE;
        String err = null;
        try {
            memberService.reg(member);
            form.setId(member.getId());
        } catch (MemberException e) {
            log.error(e.getMessage());
            isSucceed = Boolean.FALSE;
            err = ms.getMessage(e.getMessage(), null, null, Locale.getDefault());
        }
        AjaxResult rs = new AjaxResult(form, isSucceed, err);
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    @Autowired
    private MessageSource ms;

    @Autowired
    private MemberService memberService;

    public class MemberForm extends Member {

        /**
         *
         */
        private static final long serialVersionUID = 5663596084591169125L;

        private String validateCode;

        private String pwd;

        public String getValidateCode() {
            return validateCode;
        }

        public void setValidateCode(String validateCode) {
            this.validateCode = validateCode;
        }

        public String getPwd() {
            return pwd;
        }

        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }
}
